﻿using Medusa.Service.Business.Core.Entities.JCPDTCPHJLC;
using NP.BPMReportPlatform.BPMPlatform;
using NP.BPMReportPlatform.DBHelper;
using NP.BPMReportPlatform.Entities.BPM_Business;
using NP.BPMReportPlatform.Entities.BPM_Platform;
using NP.BPMReportPlatform.StandardApprovalFlow.Dto;
using NP.BPMReportPlatform.Utilities;
using NP.BPMReportPlatform.Utils;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace NP.BPMReportPlatform.StandardApprovalFlow
{
    public class CBTSDDService : BPMReportPlatformAppService, ICBTSDDService
    {
        private readonly SqlSugarClient _db;
        private readonly SqlSugarClient _dbPlatform;
        private readonly IBPMUtilsAppService _bPMUtilsApp;
        private readonly IBPMPlatformAppService _iBPMPlatformApp;
        public CBTSDDService(ISqlSugarAppService sqlSugarAppService, IBPMUtilsAppService bPMUtilsApp
            , IBPMPlatformAppService iBPMPlatformApp)
        {
            _db = sqlSugarAppService.GetBusinessInstance();
            _dbPlatform = sqlSugarAppService.GetPlatformInstance();
            _bPMUtilsApp = bPMUtilsApp;
            _iBPMPlatformApp = iBPMPlatformApp;
        }

        public async Task<PagedResultDto<CBTSDDOutput>> GetAllAsync(CBTSDDInput input)
        {
            RefAsync<int> totalCount = 0;

            var listQueryable = GetQueryable(input);
            var listViewDto = await listQueryable.ToPageListAsync(input.PageIndex, input.PageSize, totalCount);

            var userId = await _iBPMPlatformApp.GetUserAsync(CurrentUser.UserName);

            listViewDto.ForEach(x => x.Url = _bPMUtilsApp.GetProcessUrl(x.InstanceNumber, userId));

            //返回结果
            return new PagedResultDto<CBTSDDOutput>()
            {
                TotalCount = totalCount,
                Items = listViewDto
            };
        }

        public async Task<List<CBTSDDOutput>> ExportAsync(CBTSDDInput input)
        {
            var listQueryable = GetQueryable(input);
            var listViewDto = await listQueryable.ToListAsync();
            return listViewDto;
        }


        private ISugarQueryable<CBTSDDOutput> GetQueryable(CBTSDDInput input)
        {
            //var currentUserId = Guid.Empty;
            //if (!input.IsQueryAll)
            //{
            //    var currentUserDto = _dbPlatform.Queryable<Users>().Where(x => x.WorkNumber.Equals(CurrentUser.UserName)).First();
            //    if (currentUserDto != null)
            //    {
            //        currentUserId = currentUserDto.UserId;
            //    }
            //}

            var listQueryable = _db.Queryable<CommonApprovalProcess, BusinessProcessesNew>(
                    (a, b) => new JoinQueryInfos(JoinType.Left, a.InstanceNumber == b.InstanceId))
                .Where((a, b) => a.BTID == "CB-TSDD")
                .WhereIF(!string.IsNullOrEmpty(input.InstanceNumber), (a, b) => b.InstanceId == input.InstanceNumber)
                .WhereIF(input.OrderStatus != null && input.OrderStatus.Count > 0,
                    (a, b) => SqlFunc.ContainsArray(input.OrderStatus.ToArray(), b.ProcessStatusCode))
                .WhereIF(!string.IsNullOrWhiteSpace(input.AddUserName), (a, b) => a.AddUserName.Contains(input.AddUserName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Title), (a, b) => a.Text3.Contains(input.Title))
                .WhereIF(!string.IsNullOrWhiteSpace(input.CustomerCode), (a, b) => a.Text1.Contains(input.CustomerCode))
                .WhereIF(!string.IsNullOrWhiteSpace(input.OrderType), (a, b) => a.Text2.Contains(input.OrderType))
                .WhereIF(input.StartCreationTime != null && input.EndCreationTime != null,
                    (a, b) => a.AddTime >= input.StartCreationTime && a.AddTime <= input.EndCreationTime)
                .Select((a, b) => new CBTSDDOutput
                {
                    InstanceNumber = a.InstanceNumber,
                    AddTime = a.AddTime,
                    AddTimeString = a.AddTime != null && a.AddTime > DateTime.MinValue ?
                        a.AddTime.Value.ToString(DateFormatHelper.DateTimeFormat) : "",
                    AddUserName = a.AddUserName,
                    CustomerCode = a.Text1,
                    OrderType = a.Text2,
                    Title = a.Text3,
                    Content = a.TextArea1,
                    OrderStatus = b.ProcessStatusName,
                }).MergeTable().Distinct().OrderBy(ins => ins.AddTime, OrderByType.Desc);

            return listQueryable;
        }
    }
}
